﻿using BMSS.Models.Entity;
using BMSS.Models;
using SqlSugar.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BMSS.WinForm.Extensions
{
    public static class SqlsugarSetup
    {
        public static void AddSqlsugarSetup() 
        {
            // 注册 SqlSugar
            SugarIocServices.AddSqlSugar(new IocConfig()
            {
                ConnectionString = "DataSource=D:\\rxxdemo\\netprogict\\BMSS\\BMSS.WinForm\\BMSS.db",
                DbType = IocDbType.Sqlite,
                IsAutoCloseConnection = true,
            });
            AppInfo.db = DbScoped.Sugar;
            AppInfo.dbScope = DbScoped.SugarScope;
            // 生成数据库
            DbScoped.Sugar.DbMaintenance.CreateDatabase();
            // 生成数据库表
            //DbScoped.Sugar.CodeFirst.SetStringDefaultLength(200).InitTables(typeof(SysUser));
            Type[] types = typeof(SysUser).Assembly.GetTypes()
                            .Where(it => it.FullName.Contains("Entity"))//命名空间过滤，当然你也可以写其他条件过滤
                            .ToArray();
            AppInfo.db.CodeFirst.SetStringDefaultLength(200).InitTables(types);//根据types创建表  

            //var a = AppInfo.db.Queryable<SysUser>().Where(it => it.ID > 0).ToList();
            //var aa = DbScoped.Sugar.GetSimpleClient<SysUser>().AsQueryable().ToList();
        }
    }
}
